<template>
	<view class="login">
		<view class="title">
			使用账号密码登录
		</view>
		<view v-show="!isNew" class="section">
			<view><input type="text" class="input" v-model="account" placeholder="账号" /></view>
			<view><input type="password" class="input" v-model="password" placeholder="密码" /></view>
			<view class="tips">
				<span @click="newUser">注册</span>
				<!-- <span>忘记密码</span> -->
			</view>
			<view class="btn" @click="onLogin">登录</view>
		</view>
		<view class="empty">

		</view>
	</view>
</template>

<script>
	import {
		userLogin,
		setInitLocation,
		listenerTeam
	} from '../../network/login.js'
	import {
		IP
	} from '../../network/requestUrl.js'
	import {
		connect
	} from '../../utils/index.js'
	import {
		mapState
	} from 'vuex'
	import {
		dateDiffer
	} from '../../utils/index.js'
	export default {
		data() {
			return {
				isNew: false,
				account: "13751920001",
				password: "123456",
			};
		},
		watch: {
			isNew(val) {
				uni.setNavigationBarTitle({
					title: val ? '注册' : '登录'
				})
			}
		},
		onLoad() {
			this.$store.commit('setLogin', uni.getStorageSync('isLogin'))

			if (this.isLogin) {
				this.navTo()
			}
		},
		computed: {
			...mapState(['isLogin'])
		},
		methods: {
			async onLogin() {
				const result = await userLogin({
					phone: this.account,
					password: this.password
				})
				console.log(result.information)
				if (result.information.status == 0) {
					uni.showToast({
						title: result.message
					})
					return
				}
				console.log(result)
				if (!result.flag) return
				this.$store.commit('setLogin', true)
				this.$store.commit('setUserInfo', {
					...result.information,
					user_id: result.information.id,
					userName: result.information.name,
					url: result.information.url,
					phone: result.information.phone
				})
				connect()
				this.navTo()

				if (result.information.role != "家属" && result.information.location_status == 0) {
					console.log('开始监听任务')
					this.initLocation(result.information)
				}
				this.checkMemo()
			},

			async initLocation(info) {
				const location = await uni.getLocation()
				console.log(location)
				const result = await setInitLocation({
					station_id: info.station_id,
					user_id: info.id,
					lng: location[1].longitude,
					lat: location[1].latitude,
				})
				// if (!result.flag) return
				const res = await listenerTeam()

				console.log(res)
			},

			newUser() {
				uni.navigateTo({
					url: "./newUser/newUser"
				})
			},

			navTo() {
				uni.switchTab({
					url: "/pages/index/MsgList/MsgList",
					success() {
						uni.hideLoading()
					},
					fail: (err) => {
						uni.showToast({
							title: '跳转失败',
							icon: 'none'
						})
						uni.hideLoading()
						console.log(err)
					}
				})
			},
			checkMemo() {
				setTimeout(() => {
					const result = JSON
						.parse(uni.getStorageSync(this.$store.state.userInfo.user_id + 'things') || "[]")
						.filter(item => dateDiffer(item.time, Date.now()) >= 0)
					if (!!result.length) {
						uni.showToast({
							title: '您的备忘录有待查看消息',
							icon: 'none'
						})
					}
				}, 200)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login {
		height: 100vh;
		padding: 0px 60px 0;
		background-image: url('http://p1.qhimg.com/bdm/1000_1600_0/t01926f5556694157fd.jpg');
		background-size: 100% 100%;

		.title {
			padding-top: 100px;
			margin-bottom: 50px;
			text-align: center;
			font-size: 20px;
			color: #FFFFFF;
		}

		.section {
			.input {
				margin: 5px 0;
				border-bottom: 1px solid #CCCCCC;
				padding: 6px 10px;
				outline: none;
				color: #F2F2F2;
			}

			.code {
				width: 100px;
				height: 50px;
			}

			.tips {
				display: flex;
				justify-content: space-between;
				margin-top: 10px;
				color: #F2F2F2;
				font-size: 12px;

				span {
					cursor: pointer;
				}
			}

			.btn {
				padding: 10px 0;
				border-radius: 20px;
				margin-top: 20px;
				text-align: center;
				color: white;
				background-color: #1CBAB6;

				&:active {
					color: $uni-text-color;
					background-color: $uni-bg-color;
					// border: 1px solid $uni-bg-color-inverse;
				}
			}
		}

	}
</style>
